package com.cuitrip.business.home.my.card.ui;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuitrip.business.pay.model.DiscountItem;
import com.cuitrip.service.R;
import com.cuitrip.util.ResourceUtil;
import com.cuitrip.util.time.InputTime;
import com.cuitrip.util.time.OutputTime;
import com.cuitrip.util.time.TimeUtil;
import com.lab.utils.NumberUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sheena on 16/2/27.
 */
public class PromotionItemAdapter extends RecyclerView.Adapter<PromotionItemAdapter.ViewHolder>  {
    List<DiscountItem> discountItems;
    public PromotionItemAdapter() {
        discountItems=new ArrayList<>();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ct_item_card, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DiscountItem discountItem = discountItems.get(position);
        try {
            holder.tvMoney.setText(NumberUtils.getMoneyFormat(discountItem.getMoney()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        holder.tvMoneyType.setText(discountItem.getMoneyType());
        String date = discountItem.getInvalidDate();
        holder.tvInvalidDate.setText(date);
        boolean enable = true;
        try {
            long invalidDate = Long.valueOf(date);
            enable = System.currentTimeMillis() <= invalidDate;
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        holder.tvInvalidDate.setText(ResourceUtil.getString(R.string.user_attribute_id_document_expiry) + " " +
                TimeUtil.formatTime(
                        InputTime.build(discountItem.getInvalidDate()),
                        OutputTime.getLocalInstance(OutputTime.OutputType.Type_Minute)
                ));
        holder.tvInvalid.setVisibility(enable ? View.INVISIBLE : View.VISIBLE);
        holder.tvMoney.setTextColor(enable ? ResourceUtil.getThemeColor() : ResourceUtil.getColor(R.color.ganshi_ded8d7));
        holder.tvMoneyType.setTextColor(enable ? ResourceUtil.getThemeColor() : ResourceUtil.getColor(R.color.ganshi_ded8d7));
        holder.tvInvalidDate.setTextColor(enable ? ResourceUtil.getColor(R.color.qiaomai_7c706e) : ResourceUtil.getColor(R.color.ganshi_ded8d7));

    }

    @Override
    public int getItemCount() {
        return discountItems == null ? 0 : discountItems.size();
    }

    public void setDiscountItems(List<DiscountItem> discountItems) {
        this.discountItems = discountItems;
        notifyDataSetChanged();
    }



    public class ViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvMoney;
        private final TextView tvMoneyType;
        private final TextView tvInvalidDate;
        private final TextView tvInvalid;
        public ViewHolder(final View itemView) {
            super(itemView);
            tvMoney = (TextView) itemView.findViewById(R.id.tvMoney);
            tvMoneyType = (TextView) itemView.findViewById(R.id.tvMoneyType);
            tvInvalidDate = (TextView) itemView.findViewById(R.id.tvInvalidDate);
            tvInvalid = (TextView) itemView.findViewById(R.id.tvInvalid);
        }


    }


}
